/**
 * creater:kunjiang.zhou@dhc.com.cn
 * create time:2017-05-31 19:33:40
 * describe:入库
 */
APP.controller('InputController', ['$scope', '$rootScope', 'InputService', '$state', 'PopupService', '$timeout','OnlineService',
    function ($scope, $rootScope, InputService, $state, PopupService, $timeout,OnlineService) {

        /***变量声明***/
        $rootScope.data = {
            barcode: '',
            number: '',
            supplier: '',
            paper: '',
            weight: '',//克重
            specification: '',//规格
            kg: '',//重量
            axle:'' //轴序
        };
        $scope.inputList = [];

        /**
         * 初始化函数
         */
        $scope.$on('$ionicView.beforeEnter', function (e, v) {
            if (v.direction == 'back') {
                // console.log('保存前',window.localStorage.getItem('save_input'));
                console.log('前页返回');
                // console.log('v',v.direction);
                // console.log($rootScope.data);

            } else {
                $scope.init();
            }
        });
        $scope.init = function () {
            console.log('进来了没');
            $rootScope.data = {
                barcode: '',
                number: '',
                supplier: '',
                paper: '',
                weight: '',//克重
                specification: '',//规格
                kg: '',//重量
                axle:'' //轴序
            };
            // if (window.localStorage.getItem('save_input')) {
            //     $rootScope.data = window.localStorage.getItem('save_input');
            // } else {
            // }
            $scope.ifShow = false;
        };

        $scope.goSupplier = function (type) {
            $state.go('supplier', {type: type});
        };

        /**
         * 返回方法
         */
        $scope.goBack = function () {
            $scope.$ionicGoBack();
        };
        /**
         * 按条码查询物料信息
         */
        $scope.getByCode = function (barcode) {
            OnlineService.getByCode(barcode)
                .then(function (response) {
                    console.log('成功回调方法', response);
                    if (response.data.data) {
                        PopupService.showToast(response.data.data.state);
                        return;
                    }
                }, function (response) {
                    console.log('失败回调方法', response)
                })
        };
        //保存
        $scope.saveInputList = function () {
            console.log('保存前', $scope.inputList);
            var a = {
                barcode: $rootScope.data.barcode,
                number: $rootScope.data.number,
                supplier: $rootScope.data.supplier,
                paper: $rootScope.data.paper,
                weight: $rootScope.data.weight,//克重
                specification: $rootScope.data.specification,//规格
                kg: $rootScope.data.kg//重量
            };
            if (!a.barcode || !a.number ||!a.supplier ||!a.paper ||!a.weight ||!a.specification ||!a.kg){
                PopupService.showToast('请填写必要信息');
                return;
            }
            $timeout(function () {
                $scope.inputList.push(a);
            }, 200);
            window.localStorage.setItem('save_input', $scope.inputList.toString());
            PopupService.showToast('保存成功');
            $rootScope.data = {
                barcode: '',
                number: '',
                supplier: '',
                paper: '',
                weight: '',//克重
                specification: '',//规格
                kg: '',//重量
                axle:'' //轴序
            };
        };
        //是否显示提交列表
        $scope.ifShowList = function (index) {
            $rootScope.data = $scope.inputList[index];
            $scope.ifShow = !$scope.ifShow;
            $scope.inputList.splice(index,1);
            console.log($scope.inputList);
        };
        //确认提交显示列表
        $scope.confirmInput = function () {
            if (!$scope.inputList.length) {
                PopupService.showToast('请完善信息');
            } else {
                console.log('window.localStorage',window.localStorage.getItem('save_input').split(','));
                console.log(typeof window.localStorage.getItem('save_input'));
                $scope.ifShow = !$scope.ifShow;
            }
            // if (window.localStorage.getItem('save_input')) {
            //     var localList = window.localStorage.getItem('save_input');
            //     $scope.inputList = localList;
            //     var a = $scope.inputList.split(",");
            // }

        };
        //入库方法
        $scope.inStock = function () {
            InputService.inStock($scope.inputList)
                .success(function (response) {
                    if (response.success) {
                        PopupService.showToast('入库成功');
                    }
                    $scope.ifShow = !$scope.ifShow;
                    $rootScope.data = {
                        barcode: '',
                        number: '',
                        supplier: '',
                        paper: '',
                        weight: '',//克重
                        specification: '',//规格
                        kg: '',//重量
                        axle:'' //轴序
                    };
                    $scope.inputList = [];
                })
        }


    }
]);

/**
 * creater:kunjiang.zhou@dhc.com.cn
 * create time:2017-05-31 19:33:40
 * describe:入库
 */
APP.service('InputService', ['$http', 'UrlService', function ($http, UrlService) {
    /**
     * 入库方法
     */
    this.inStock = function (data) {
        var params = {
            barcode:data.barcode,
            number:data.number,
            supplier:data.supplier,
            paper:data.paper,
            weight:data.weight,
            specification:data.specification,
            kg:data.kg
        };
        return $http.post(UrlService.getUrl('IN_STOCK'), data);
    }

}]);
